package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.Address;
import com.ruoyi.project.system.mapper.AddressMapper;
import com.ruoyi.project.system.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 * @author hongwj
 * @date 2024-03-26
 */
@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public Address selectAddressById(Long id) {
        return addressMapper.selectAddressById(id);
    }

    /**
     * 查询收货地址列表
     *
     * @param address 收货地址
     * @return 收货地址
     */
    @Override
    public List<Address> selectAddressList(Address address) {
        return addressMapper.selectAddressList(address);
    }

    /**
     * 新增收货地址
     *
     * @param address 收货地址
     * @return 结果
     */
    @Override
    public int insertAddress(Address address) {
        address.setCreateBy(SecurityUtils.getUserId().toString());
        address.setCreateTime(DateUtils.getNowDate());
        if (address.isDefaultAddress()) {
            addressMapper.updateDefaultAddress(address.getCreateBy(), address.getId());
        }
        return addressMapper.insertAddress(address);
    }

    /**
     * 修改收货地址
     *
     * @param address 收货地址
     * @return 结果
     */
    @Override
    public int updateAddress(Address address) {
        if (address.isDefaultAddress()) {
            addressMapper.updateDefaultAddress(address.getCreateBy(), address.getId());
        }
        return addressMapper.updateAddress(address);
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Long[] ids) {
        return addressMapper.deleteAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressById(Long id) {
        return addressMapper.deleteAddressById(id);
    }
}
